/*
 * Title: uk.co.droidinactu.sony.prst1.home.Utils
 * Copyright (c) 2008
 * Company: EADS-DS UK
 * Author: aspela
 * $Id$
 *
 * Package: uk.co.droidinactu.sony.prst1.home
 *
 */
package uk.co.droidinactu.sony.prst1.home;

import android.os.Environment;

/**
 * CLASS DOCUMENTATION _MUST_ BE GIVEN HERE. ENSURE RELEVENT @see javadoc
 * REFERENCES ARE USED IF REQUIRED.
 * 
 * @author aspela
 */
public final class Utils {

    public static boolean isSdMounted(final boolean requireWriteAccess) {
        // TODO: After fix the bug, add "if (VERBOSE)" before logging errors.
        final String state = Environment.getExternalStorageState();
        // Log.v(DataModel.LOG_TAG, "storage state is " + state);

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // if (requireWriteAccess) {
            // boolean writable = checkFsWritable();
            // Log.v(TAG, "storage writable is " + writable);
            // return writable;
            // } else {
            return true;
            // }
        } else if (!requireWriteAccess && Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }

    public static boolean isSdPresent() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }

    /**
     * Reallocates an array with a new size, and copies the contents of the old
     * array to the new array.
     * 
     * @param oldArray
     *            the old array, to be reallocated.
     * @param newSize
     *            the new array size.
     * @return A new array with the same contents.
     */
    public static Object resizeArray(final Object oldArray, final int newSize) {
        final int oldSize = java.lang.reflect.Array.getLength(oldArray);
        final Class elementType = oldArray.getClass().getComponentType();
        final Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);
        final int preserveLength = Math.min(oldSize, newSize);
        if (preserveLength > 0) {
            System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
        }
        return newArray;
    }
}
